<template>
    <div style="height: 100%">
        <div slot="toolheader" foldbtn="false" border="false">
            <div class="table-operator">
                <a-button @click="addRow">
                    {{$ti18('rxList.add','新增')}}
                </a-button>
            </div>
        </div>
        <rx-grid ref="table"
                 style="height: 600px;"
                 :dataSource="pmtFields"
                 :columns="pmtColumns"
                 id-field="pmtId"
                 :allowRowSelect="false"
                 :showPage="false"
                 :bordered="true"
                 :showExpandedRowRender="true"
                 :expanRowKeys="expanRowKeys"
                 @rowExpand="rowExpand"
        >
            <span slot="alias" slot-scope="{text,record,index,blur}">
                <a-input v-model="record.alias" @blur="changeAlias('alias',text,record,index,blur)" />
            </span>
            <span slot="name" slot-scope="{text,record,index,blur}">
                <a-input v-model="record.name" @blur="blur(index,'name',text,record)" />
            </span>
            <span slot="status" slot-scope="{ text, record }">
                <a-tag :color="statusMap[text].color">
              <span>{{ statusMap[text].text }}</span>
            </a-tag>
            </span>
            <span slot="action" slot-scope="{ text, record }" class="actionIcons">
                <span  @click.stop="removeRow(record)">
                    {{$ti18('rxList.delBtn','删除')}}
                </span>
                <span v-if="!record.menuId" @click.stop="deployMenu(record)">
                    {{$ti18('rxList.deployMenu','发布菜单')}}
                </span>
            </span>
            <template slot="expandedRowRender" slot-scope="{record,index}">
                <a-tabs style="height: 350px ;width: calc(100% - 20px);margin-left: 10px" defaultActiveKey="buttonField" :tabBarStyle="{borderBottom:0}">
                    <a-tab-pane forceRender :tab="i18n('form.buttonPower','按钮权限')" key="buttonField">
                        <rx-fit style="padding-top: 8px;">
                            <div slot="toolheader" foldbtn="false" border="false">
                                <div class="table-operator">
                                    <a-button @click="reloadButtons">
                                        {{i18n('isButton.reloadButton','重新加载按钮')}}
                                    </a-button>
                                    <a-button @click="removeButtons">
                                        {{i18n('isButton.deleteButton','删除按钮')}}
                                    </a-button>
                                </div>
                            </div>
                            <rx-grid :ref="'table_button_'+record.pmtId" :dataSource="grantButtonFields[index]"
                                     :columns="grantButtonColumns" :allowRowSelect="true" :multiSelect="true" :showPage="false"
                                     :bordered="true" id-field="btnName">
                            </rx-grid>
                        </rx-fit>
                    </a-tab-pane>
                    <a-tab-pane forceRender :tab="i18n('list.dataPermission','数据权限')" key="dataList">
                        <rx-fit style="padding-top: 8px;">
                            <div slot="toolheader" foldbtn="false" border="false">
                                <div class="table-operator">
                                    <a-button @click="addAndGrantRow">{{i18n('isButton.addAndGrantRow','新增条件分组')}}</a-button>
                                    <a-button @click="upRow">{{i18n('isButton.upRow','上移')}}</a-button>
                                    <a-button @click="downRow">{{i18n('isButton.downRow','下移')}}</a-button>
                                    <a-button @click="addDataFieldGrantRow">{{i18n('isButton.addFieldPermiss','新增字段权限')}}</a-button>
                                    <a-button @click="addDataUserGrantRow">{{i18n('isButton.addUserPermiss','新增用户权限')}}</a-button>
                                    <a-button @click="addDataGroupGrantRow">{{i18n('isButton.addGroupPermiss','新增组权限')}}</a-button>
                                    <a-button @click="addDataInstGrantRow">{{i18n('isButton.addInstPermiss','新增机构权限')}}</a-button>
                                    <a-button @click="addDataCreateUserGrantRow">{{i18n('isButton.addCreateUserPermiss','新增创建人权限')}}</a-button>
                                    <a-button @click="addDataCreateGroupGrantRow">{{i18n('isButton.addCreateDeptPermiss','新增创建部门权限')}}</a-button>
                                    <a-button @click="addDataCreateInstGrantRow">{{i18n('isButton.addCreateInstPermiss','新增创建机构权限')}}</a-button>
                                    <a-button @click="removeGrantRow">{{i18n('isButton.delPermiss','删除权限')}}</a-button>
                                </div>
                            </div>
                            <rx-grid style="height: 600px;"
                                :ref="'table_list_'+record.pmtId" :dataSource="grantListFields[index]"
                                :columns="grantColumns" :allowRowSelect="true" :multiSelect="true" :showPage="false"
                                :bordered="true">
                                    <span slot="type" slot-scope="{text,record,index,blur}">
                                      <a-select labelInValue v-model="record.typeExt" @blur="blur(index,'type',record.typeExt,record)" v-if="record.isGroup==true">
                                        <a-select-option value="conditionAnd">{{i18n('options.conditionAnd','按AND分组')}}</a-select-option>
                                        <a-select-option value="conditionOr">{{i18n('options.conditionOr','按OR分组')}}</a-select-option>
                                      </a-select>
                                      <tepmate v-else>
                                        {{record.type_name}}
                                      </tepmate>
                                    </span>
                                                            <span slot="tablePre" slot-scope="{text,record,index,blur}" v-if="record.type!='conditionAnd' && record.type!='conditionOr'">
                                      <a-input v-model="record.tablePre" @blur="blur(index,'tablePre',text,record)" />
                                    </span>
                                                            <span slot="desc" slot-scope="{text,record,index,blur}" v-if="record.type!='conditionAnd' && record.type!='conditionOr'">
                                      <a-input v-model="record.desc" @blur="blur(index,'desc',text,record)" />
                                    </span>
                                                            <span slot="field" slot-scope="{text,record,index,blur}" v-if="record.type!='conditionAnd' && record.type!='conditionOr'">
                                      <a-select labelInValue v-model="record.fieldExt" @blur="changeFieldName('field',record.fieldExt,record,index,blur)" :options="fieldDatas"></a-select>
                                    </span>
                                                            <span slot="op" slot-scope="{text,record,index,blur}" v-if="record.type!='conditionAnd' && record.type!='conditionOr'">
                                      <a-select labelInValue v-model="record.opExt" @blur="blur(index,'op',record.opExt,record)">
                                        <a-select-option value="=">{{i18n('options.equal','等于')}}</a-select-option>
                                        <a-select-option value="in">{{i18n('options.range','范围')}}</a-select-option>
                                      </a-select>
                                    </span>
                                                            <span slot="scope" slot-scope="{text,record,index,blur}" v-if="record.type!='conditionAnd' && record.type!='conditionOr'">
                                      <a-select v-if="record.type=='user' && record.op=='='" labelInValue v-model="record.scopeExt" @blur="blur(index,'scope',record.scopeExt,record)">
                                              <a-select-option value="SELF">{{i18n('options.oneself','自己')}}</a-select-option>
                                              <a-select-option value="DUP_USERS">{{i18n('options.dupUsers','直属上级用户')}}</a-select-option>
                                              <a-select-option value="UP_USERS">{{i18n('options.upUsers','所有上级用户')}}</a-select-option>
                                              <a-select-option value="DDOWN_USERS">{{i18n('options.ddownUsers','直属下级用户')}}</a-select-option>
                                              <a-select-option value="DOWN_USERS">{{i18n('options.downUsers','所有下级用户')}}</a-select-option>
                                               <a-select-option value="CHARGE_DEPT_USERS">{{i18n('options.chargeDeptUsers','负责人的组内用户')}}</a-select-option>
                                      </a-select>
                                      <a-select v-if="record.type=='group' && record.op=='='" labelInValue v-model="record.scopeExt" @blur="blur(index,'scope',record.scopeExt,record)">
                                              <a-select-option value="SELF_DEP">{{i18n('options.selfDep','默认部门')}}</a-select-option>
                                              <a-select-option value="SELF_DOWN">{{i18n('options.selfDown','当前人部门(下级)')}}</a-select-option>
                                              <a-select-option value="DUP_DEPS">{{i18n('options.dupDeps','直属上级部门')}}</a-select-option>
                                              <a-select-option value="UP_DEPS">{{i18n('options.upDeps','所有上级部门')}}</a-select-option>
                                              <a-select-option value="DDOWN_DEPS">{{i18n('options.ddownDeps','本部门及下级部门')}}</a-select-option>
                                              <a-select-option value="DOWN_DEPS">{{i18n('options.downDeps','所有下级部门')}}</a-select-option>
                                      </a-select>
                                      <a-select v-if="record.type=='inst' && record.op=='='" labelInValue v-model="record.scopeExt" @blur="blur(index,'scope',record.scopeExt,record)">
                                              <a-select-option value="SELF_TENANT">{{i18n('options.selfTenant','作者机构')}}</a-select-option>
                                              <a-select-option value="DUP_TENANTS">{{i18n('options.dupTenants','上级机构')}}</a-select-option>
                                              <a-select-option value="UP_TENANTS">{{i18n('options.upTenants','所有上级机构')}}</a-select-option>
                                              <a-select-option value="DDOWN_TENANTS">{{i18n('options.ddownTenants','直属下级机构')}}</a-select-option>
                                              <a-select-option value="DOWN_TENANTS">{{i18n('options.downTenants','所有下级机构')}}</a-select-option>
                                      </a-select>
                                      <a-input v-model="record.scope" @blur="blur(index,'scope',text,record)" v-if="record.type=='field' && record.op=='=' && (record.dataType==null || record.dataType=='string' || record.dataType=='varchar' || record.dataType=='clob')" />
                                      <a-input-number v-model="record.scope" @blur="blur(index,'scope',text,record)" v-if="record.type=='field' && (record.dataType=='int' || record.dataType=='number')" />
                                      <a-date-picker showTime format="YYYY-MM-DD" @change="gridChangeDate($event,$event,{record:record,dataIndex:'scope',format:'YYYY-MM-DD'})" @ok="blur(index,'scope',text,record)" v-if="record.type=='field' && record.dataType=='date'" />

                                      <rx-input-button @click="selUserData($event,{field:'scope',text:text,record:record,index:index,blur:blur})" v-if="record.type=='user' && record.op=='in'" />
                                      <rx-input-button @click="selGroupData($event,{field:'scope',text:text,record:record,index:index,blur:blur})" v-if="record.type=='group' && record.op=='in'" />
                                      <rx-input-button @click="selTenantData($event,{field:'scope',text:text,record:record,index:index,blur:blur})" v-if="record.type=='inst' && record.op=='in'" />
                                      <rx-input-button :value="rangDlg" @click="rangSetDlg($event,{field:'scope',text:text,record:record,index:index,blur:blur})" v-show="record.type=='field' && record.op=='in' && (record.dataType==null || record.dataType=='string' || record.dataType=='varchar' || record.dataType=='clob')"  />
                                    </span>
                                                            <span slot="scope2" slot-scope="{text,record,index,blur}" v-if="record.type!='conditionAnd' && record.type!='conditionOr'">
                                      <a-input-number v-model="record.scope2" @blur="blur(index,'scope2',text,record)" v-if="record.type=='field' && record.op=='in' && (record.dataType=='int' || record.dataType=='number')" />
                                      <a-date-picker showTime format="YYYY-MM-DD" @change="gridChangeDate($event,$event,{record:record,dataIndex:'scope2',format:'YYYY-MM-DD'})" @ok="blur(index,'scope2',text,record)" v-if="record.type=='field' && record.op=='in' && record.dataType=='date'" />
                                      <a-select  mode="tags" labelInValue :options="getSelectData(record)" @change="gridChangeSelect($event,record,'scope2')" @blur="blur(index,'scope2',text,record)" v-if="record.scope=='selfData' && record.type=='field' && record.op=='in' && (record.dataType==null || record.dataType=='string' || record.dataType=='varchar' || record.dataType=='clob')" />
                                      <rx-select :multiSelect="true" :labelInValue="true" @change="gridChangeSelect($event,record,'scope2')" @blur="blur(index,'scope2',text,record)" ajaxType="post" :url="'/api-form/form/core/formCustomQuery/queryForJson_'+record.configs.sql" data-field="data" :valueField="record.configs.valueField" :textField="record.configs.textField" v-if="record.scope=='sql' && record.type=='field' && record.op=='in' && (record.dataType==null || record.dataType=='string' || record.dataType=='varchar' || record.dataType=='clob')" />
                                      <rx-select :multiSelect="true" :labelInValue="true" @change="gridChangeSelect($event,record,'scope2')" @blur="blur(index,'scope2',text,record)" :url="'/api-system/system/core/sysDic/getByAlias?alias='+record.configs.dic+'&i18nTransform=true'" value-field="value" text-field="name" v-if="record.scope=='dic' && record.type=='field' && record.op=='in' && (record.dataType==null || record.dataType=='string' || record.dataType=='varchar' || record.dataType=='clob')" />
                                      <rx-input-button @click="showDialog($event,{field:'scope2',text:text,record:record,index:index,blur:blur})" v-if="record.scope=='dialog' && record.type=='field' && record.op=='in' && (record.dataType==null || record.dataType=='string' || record.dataType=='varchar' || record.dataType=='clob')" />
                                    </span>
                            </rx-grid>
                        </rx-fit>
                    </a-tab-pane>
                    <a-tab-pane forceRender :tab="i18n('list.fieldPermiss','字段权限')" key="dataField">
                        <rx-fit style="padding-top: 8px;">
                            <div slot="toolheader" foldbtn="false" border="false">
                                <div class="table-operator">
                                    <a-button @click="reloadFields">{{i18n('isButton.reloadField','重新加载字段')}}</a-button>
                                    <a-button @click="removeFields">{{i18n('isButton.removeFields','删除字段')}}</a-button>
                                </div>
                            </div>
                            <rx-grid :ref="'table_field_'+record.pmtId" :dataSource="grantFieldFields[index]"
                                     :columns="grantFieldColumns" :allowRowSelect="true" :multiSelect="true" :showPage="false"
                                     :bordered="true" id-field="field">
                            </rx-grid>
                        </rx-fit>
                    </a-tab-pane>
                </a-tabs>
            </template>
        </rx-grid>
    </div>
</template>

<script>
import FormBoPmtApi from '@/api/form/core/formBoPmt';
import {Util,Dialog} from "@lowcode/jpaas-common-lib";
import DialogBox from "@/assets/js/DialogBox";
export default {
    name: "FormBoListEditDataConf",
    props:{
        pkId:{
            type:String
        },
        fieldDatas:{
            type:Array,
            default:[]
        },
        buttonConf:{
            type:Array,
            default:[]
        },
        calendarView:{
            type:Object
        }
    },
    data(){
        return {
            loadStart:false,
            rangDlg:{},
            expanRowKeys: [],
            currentObject:{},
            statusMap: {
                'YES': {color: 'green', text: this.i18n('isStatusMap.yes','是')},
                'NO': {color: 'blue', text:  this.i18n('isStatusMap.no','否')}
            },
            pmtFields: [],
            pmtColumns: [
                {title: this.i18n('form.markKey','标识键'), dataIndex: 'alias', allowCellEdit: true, scopedSlots: {customRender: 'alias'}},
                {title: this.$ti18('rxForm.name','名称'), dataIndex: 'name', allowCellEdit: true, scopedSlots: {customRender: 'name'}},
                {title: this.i18n('form.whetherToPublish','是否发布'), dataIndex: 'status', scopedSlots: {customRender: 'status'}},
                {title: this.i18n('form.menuName','菜单名称'), dataIndex: 'menuName'},
                {title: this.$ti18('rxList.action','操作'), dataIndex: 'action', scopedSlots: {customRender: 'action'}}
            ],
            buttons:[],
            columns:[],
            grantButtonColumns: [{
                title: this.i18n('form.buttonName','按钮名称'),
                dataIndex: 'btnLabel',
                width: 180
            }, {
                title: this.i18n('form.buttonMark','按钮标识'),
                dataIndex: 'btnName',
                width: 180
            }],
            grantFieldColumns: [{
                title: this.i18n('form.fieldName','字段名称'),
                dataIndex: 'header',
                width: 180
            }, {
                title: this.i18n('form.fieldKey','字段Key'),
                dataIndex: 'field',
                width: 180
            }, {
                title: this.i18n('form.defaultOrNot','是否默认'),
                dataIndex: 'isDefault',
                type: 'checkboxColumn',
                scopedSlots: {customRender: 'isDefault'},
                width:180,
                align:'center'
            }],
            grantButtonFields:[],
            grantListFields:[],
            grantFieldFields:[],
            grantColumns: [{title: this.i18n('form.permissionType','权限类型'), dataIndex: 'type',displayField: 'type_name',allowCellEdit:true,scopedSlots: {customRender: 'type'}},
                {title: this.i18n('form.permissionDesc','权限描述'), dataIndex: 'desc', allowCellEdit: true, scopedSlots: {customRender: 'desc'}},
                {title: this.i18n('form.tablePrefix','表前缀'), dataIndex: 'tablePre', allowCellEdit: true, scopedSlots: {customRender: 'tablePre'}, width:120},
                {
                    title: this.i18n('form.filterField','筛选字段'),
                    dataIndex: 'field',
                    displayField: 'field_name',
                    allowCellEdit: true,
                    scopedSlots: {customRender: 'field'}
                },
                {
                    title: this.i18n('form.comparator','比较符'),
                    dataIndex: 'op',
                    displayField: 'op_name',
                    allowCellEdit: true,
                    scopedSlots: {customRender: 'op'}
                },
                {
                    title: this.i18n('form.scope1','作用域1'),
                    dataIndex: 'scope',
                    displayField: 'scope_name',
                    allowCellEdit: true,
                    scopedSlots: {customRender: 'scope'}
                },
                {
                    title: this.i18n('form.scope2','作用域2'),
                    dataIndex: 'scope2',
                    displayField: 'scope2_name',
                    allowCellEdit: true,
                    scopedSlots: {customRender: 'scope2'}
                }]
        }
    },
    created() {
        this.loadData({buttons:this.buttonConf,columns:this.fieldDatas});
    },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"FormCalendarView",key);
        },
        loadData(params){
            if(params) {
                this.buttons = params.buttons;
                this.columns = params.columns;
            }
            FormBoPmtApi.getCalendarPmt(this.pkId).then(res=>{
                this.loadStart=true;
                if(res.data){
                    if(res.data.list.length>0) {
                        this.expanRowKeys = [res.data.list[0].pmtId];
                    }
                    this.pmtFields=res.data.list;
                    for(var i=0;i<this.pmtFields.length;i++){
                        this.grantButtonFields[i]=JSON.parse(this.pmtFields[i].buttons);
                        this.grantListFields[i]=JSON.parse(this.pmtFields[i].datas);
                        this.grantFieldFields[i]=JSON.parse(this.pmtFields[i].fields);
                    }
                }
            })
        },
        getData(){
            var data=this.$refs.table.getData();
            for(var i=0;i<data.length;i++){
                if(this.$refs["table_button_"+data[i].pmtId]) {
                    data[i].buttons = this.$refs["table_button_" + data[i].pmtId].getData();
                    data[i].datas = this.$refs["table_list_" + data[i].pmtId].getData();
                    data[i].fields = this.$refs["table_field_" + data[i].pmtId].getData();
                }
            }
            return {loadStart:this.loadStart,data:data};
        },
        rowExpand(expanded,record){
            if(expanded){
                this.expanRowKeys=[record.pmtId];
            }
        },
        parseGrantDataSource(data,field){
            if(field=='buttons'){
                return this.buttons;
            }else if(field=='fields'){
                return this.columns.map(item=>{
                    return {header:item.label,field:item.value,isDefault:false};
                });
            }
        },
        addRow(){
            var pmtId=Util.randomId();
            this.$refs.table.addRow({pmtId:pmtId,boListId:this.pkId,status:'NO',isNew:true});
            this.expanRowKeys=[pmtId];
            this.grantButtonFields.push(this.buttons.map(item=>{
                return {btnLabel:item.btnLabel,btnName:item.btnName};
            }));
            this.grantListFields.push([]);
            this.grantFieldFields.push(this.columns.map(item=>{
                return {header:item.label,field:item.value,isDefault:false};
            }));
        },
        removeRow(record) {
            var self_=this;
            this.$confirm({
                title: this.i18n('form.operationTips','操作提示'),
                zIndex:20000,
                content: record.menuId?this.i18n('form.menuHasBeenPublished','已发布菜单，删除后菜单展示列表将不控制权限'):
                    this.i18n('form.deleteRecord','您确定需要删除记录吗？'),
                okText: this.$ti18('rxForm.okBtn','确认'),
                cancelText: this.$ti18('rxForm.cancelBtn','取消'),
                onOk() {
                    if(record.pmtId && record.menuId){
                        FormBoPmtApi.del(record.pmtId).then(res=>{
                            if(res.success) {
                                self_.$refs.table.removeRows([record.pmtId]);
                            }
                        })
                    }else {
                        self_.$refs.table.removeRows([record.pmtId]);
                    }

                },
                onCancel() {
                }
            })

        },
        removeGrantRow(){
            var table=this.$refs["table_list_"+this.expanRowKeys[0]];
            table.removeSelected();
            table.$forceUpdate();
        },
        deployMenu(record){
            var self_=this;
            var component = 'modules/form/core/FormCalendarViewShow'
            DialogBox.openDeploymenuDialog(
                {
                    releaseId: this.calendarView.ID_,
                    name: this.calendarView.name+'_'+record.name,
                    key: this.calendarView.key+'_'+record.alias+'_Calendar',
                    boListKey: this.calendarView.key,
                    component: component,
                    settingType:"custom",
                    params: {calendarKey: this.calendarView.key},
                },
                {
                    curVm: this,
                    widthHeight: ['800px', '600px'],
                },function(data){
                    record.menuId=data.id;
                    record.menuName=data.name;
                    record.status='YES';
                    if(self_.$refs["table_button_" + self_.expanRowKeys[0]]) {
                        record.buttons = JSON.stringify(self_.$refs["table_button_" + self_.expanRowKeys[0]].getData());
                        record.datas = JSON.stringify(self_.$refs["table_list_" + self_.expanRowKeys[0]].getData());
                        record.fields = JSON.stringify(self_.$refs["table_field_" + self_.expanRowKeys[0]].getData());
                    }
                    if(record.isNew){
                        delete record.pmtId;
                    }
                    FormBoPmtApi.save(record).then(res=>{
                        self_.loadData({buttons:self_.buttons,columns:self_.columns});
                    });
                }
            )
        },
        addAndGrantRow(){
            this.addTreeRow({typeExt:{key:'conditionAnd',label:'按AND分组'},type: 'conditionAnd', type_name: '按AND分组',isGroup:true})
        },
        //数据权限表格方法
        addDataFieldGrantRow() {
            this.addTreeRow({fieldExt:'',opExt:'',scopeExt:'',type: 'field', type_name: '按字段过滤',op:'=',op_name:'等于'})
        },
        addDataUserGrantRow() {
            this.addTreeRow({fieldExt:'',opExt:'',scopeExt:'',type: 'user', type_name: '按用户过滤',op:'=',op_name:'等于'})
        },
        addDataGroupGrantRow() {
            this.addTreeRow({fieldExt:'',opExt:'',scopeExt:'',type: 'group', type_name: '按用户组过滤',op:'=',op_name:'等于'})
        },
        addDataInstGrantRow() {
            this.addTreeRow({fieldExt:'',opExt:'',scopeExt:'',type: 'inst', type_name: '按机构过滤',op:'=',op_name:'等于'})
        },
        addDataCreateUserGrantRow() {
            this.addTreeRow({fieldExt:'',opExt:'',scopeExt:'',type: 'user', type_name: '按用户过滤', field: 'CREATE_BY_', field_name: '创建人ID',op:'=',op_name:'等于'})
        },
        addDataCreateGroupGrantRow() {
            this.addTreeRow({fieldExt:'',opExt:'',scopeExt:'',type: 'group', type_name: '按用户组过滤', field: 'CREATE_DEP_ID_', field_name: '组ID',op:'=',op_name:'等于'})
        },
        addDataCreateInstGrantRow() {
            this.addTreeRow({fieldExt:'',opExt:'',scopeExt:'',type: 'inst', type_name: '按机构过滤', field: 'TENANT_ID_', field_name: '租户ID',op:'=',op_name:'等于'})
        },
        addGrantRow(row) {
            this.$refs["table_list_"+this.expanRowKeys[0]].addRow(row);
        },
        addTreeRow(row){
            var table=this.$refs["table_list_"+this.expanRowKeys[0]];
            var selRow=table.getSelectedRow();
            if(!selRow){
                this.addGrantRow(row);
                return;
            }
            if(selRow.type!='conditionAnd' && selRow.type!='conditionOr'){
                this.$message.error(this.i18n('nextCondi',"只能加在条件分组下面！"));
                return;
            }
            if(!selRow.children){
                selRow.children=[];
            }
            row[table.parentField]=selRow[table.idField];
            table.addItem(selRow.children,row);
            table.$forceUpdate();
        },
        selUserData(vm,e){
            Dialog.openUserDialog({
                curVm: this, data: {single: false}, widthHeight: ['800px', '600px']
            }, function (self, data) {
                var ids=[];
                var names=[];
                for(var i=0;i<data.length;i++){
                    ids.push(data[i].userId);
                    names.push(data[i].fullName);
                }
                e.record[e.field]=ids.join(',');
                e.record[e.field+'_name']=names.join(',');
                e.blur(e.index,e.field,e.text,e.record);
            })
        },
        selGroupData(vm,e){
            Dialog.openGroupDialog({
                curVm: this, data: {single: false}, widthHeight: ['900px', '600px']
            }, function (self, data) {
                var ids=[];
                var names=[];
                for(var i=0;i<data.length;i++){
                    ids.push(data[i].groupId);
                    names.push(data[i].name);
                }
                e.record[e.field]=ids.join(',');
                e.record[e.field+'_name']=names.join(',');
                e.blur(e.index,e.field,e.text,e.record);
            })
        },
        selTenantData(vm,e){

        },
        rangSetDlg(vm,e){
            var config = {curVm: this, widthHeight: ['800px', '600px'],
                data:{scope:e.record[e.field],scope_name:e.record[e.field+'_name'],configs:e.record['configs']}};
            var self = this ;
            DialogBox.openFieldRangConfig(config,function(data){
                e.record[e.field]=data.scope;
                e.record[e.field+'_name']=data.scope_name;
                e.record['configs']=data.configs;
                e.blur(e.index,e.field,e.text,e.record);
                var obj = {};
                obj.text = e.record.scope_name ;
                obj.value = ''
                self.rangDlg = obj;
            })
        },
        gridChangeSelect(value,record,field){
            var keys=[];
            var labels=[];
            for(var i=0;i<value.length;i++){
                keys.push(value[i].key);
                labels.push(value[i].label);
            }
            record[field]=keys.join(',');
            record[field+'_name']=labels.join(',');
        },
        getSelectData(record){
            return JSON.parse(record.configs.selDatas);
        },
        showDialog(vm,e){
            DialogBox.dialog({key:e.record.configs.dialog},{
                curVm: vm
            },function(action,data){
                var rows=data.rows || data;
                var labels=[];
                var values=[];
                for(var i=0;i<rows.length;i++){
                    labels.push(rows[i][e.record.configs.textField]);
                    values.push(rows[i][e.record.configs.valueField]);
                }
                e.record[e.field]=values.join(',');
                e.record[e.field+'_name']=labels.join(',');
                e.blur(e.index,e.field,e.text,e.record);
            });
        },
        upRow() {
            this.$refs["table_list_"+this.expanRowKeys[0]].moveUp();
        },
        downRow() {
            this.$refs["table_list_"+this.expanRowKeys[0]].moveDown();
        },
        gridChangeDate(date, dateString, e) {
            e.record[e.dataIndex] = date.format(e.format);
        },
        changeFieldName(column,text,record,index,blur){
            for(var i=0;i<this.fieldDatas.length;i++){
                if(this.fieldDatas[i].value==text.key){
                    record.dataType=this.fieldDatas[i].key;
                    break;
                }
            }
            record.fieldLabel=text.label;
            blur(index,column,text,record)
        },
        changeAlias(column,text,record,index,blur){
            var ary=this.pmtFields.filter(item=>item.pmtId!=record.pmtId && text==item.alias);
            if(ary.length>0){
                text="";
                record.alias="";
            }
            blur(index,column,text,record);
        },
        reloadButtons(){
            this.$refs['table_button_'+this.expanRowKeys[0]].dataAction=this.buttons.map(item=>{
                return {btnLabel:item.btnLabel,btnName:item.btnName};
            });
        },
        removeButtons(){
            this.$refs['table_button_'+this.expanRowKeys[0]].removeRows();
        },
        reloadFields(){
            var ary=this.$refs['table_field_'+this.expanRowKeys[0]].dataAction;
            this.$refs['table_field_'+this.expanRowKeys[0]].dataAction=this.columns.map(item=>{
                var obj=ary.filter(aryItem=>aryItem.field==item.field);
                return {header:item.label,field:item.value,isDefault:obj&&obj.length>0?obj[0].isDefault?true:false:false};
            })
        },
        removeFields(){
            this.$refs['table_field_'+this.expanRowKeys[0]].removeRows();
        }
    }
}
</script>

<style scoped>

</style>